const app = getApp();
const api = require('../../api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // domain: api.xh.domain,
    domain: api.xh.downloadDomain,
    grade_id: '',
    course_id: '',
    knowledg_ids: '',

    selectGradeV: '',
    selectSubjectV: '',
    selectKnowledgeV: '',
    showV: false,
    knowledgeShowV: false,
    selectedV: 0,
    selectedKnowledgeV: 0,
  },

  toggleCourseV: function () {
    var page = this;
    page.setData({
      showV: !page.data.showV,
      knowledgeShowV: page.data.knowledgeShowV ? !page.data.knowledgeShowV : page.data.knowledgeShowV,
    });

  },

  toggleKnowledgeV: function () {
    var page = this;
    page.setData({
      knowledgeShowV: !page.data.knowledgeShowV,
      showV: page.data.showV ? !page.data.showV : page.data.showV,
    });

  },

  sureChooseV: function () {
    var page = this;
    page.setData({
      showV: false,
      treeData: [],
    });

    //  知识点
    if (page.data.course_id !== '' && page.data.grade_id !== '') {
      wx.request({
        url: api.public.public_knowledg_list,
        data: {
          grade_id: page.data.grade_id,
          course_id: page.data.course_id,
        },
        success(knowledg_res) {
          if (JSON.stringify(knowledg_res.data) != '{}') {
            // 设置数据  
            page.setData({
              treeData: knowledg_res.data.data.list
            });

          } else {
            page.setData({
              treeData: [],
              knowledg_ids: '',
              selectKnowledgeV: ''
            });
          }
        }
      });
    } else {
      page.setData({
        treeData: [],
        knowledg_ids: '',
        selectKnowledgeV: ''
      });
    }

    //  视频列表
    this.getVideoList();
  },

  sureChooseVKnowledge: function () {
    var page = this;
    page.setData({
      knowledgeShowV: false
    });
    //  视频列表
    this.getVideoList();
  },

  chooseKnowledgeV: function (e) {
    var page = this;
    if (page.data.selectedKnowledgeV == 0) {
      page.setData({
        selectedKnowledgeV: 1
      });
    }
    page.setData({
      selectKnowledgeV: e.currentTarget.dataset.name,
      knowledg_ids: e.currentTarget.dataset.select
    });

  },

  chooseGradeV: function (e) {
    var page = this;
    if (page.data.selectedV == 0) {
      page.setData({
        selectedV: 1
      });
    }

    page.setData({
      selectGradeV: e.currentTarget.dataset.select == '' ? '全部' : e.currentTarget.dataset.name,
      grade_id: e.currentTarget.dataset.select
    });

  },

  chooseSubjectV: function (e) {
    var page = this;
    if (page.data.selectedV == 0) {
      page.setData({
        selectedV: 1
      });
    }
    page.setData({
      selectSubjectV: e.currentTarget.dataset.select == '' ? '' : e.currentTarget.dataset.name,
      course_id: e.currentTarget.dataset.select
    });
  },


  toVideoDetail: function (e) {
    var video_id = e.currentTarget.dataset.id;
    var logging = wx.getStorageSync("_logining");
    if (!logging) {
      app.toAuth('/' + this.route);
    } else {
      wx.navigateTo({
        url: '../video_preview/video_preview?video_id=' + video_id,
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var page = this;
    page.getVideoList();
    //  年级学科列表
    app.sendRequest({
      url: api.public.grade_course_student,
      success: function (res) {
        if (res.data != undefined && res.data != '') {
          // 设置数据  
          page.setData({
            course: res.data.list,
            subject: res.data.list[0].course_list,
          })
        }

      },
    });
    if (page.data.course_id === '' && page.data.grade_id === '') {
      page.setData({
        treeData: [],
      });
    }


  },

  getVideoList: function () {
    var page = this;
    page.setData({ toastHidden: false });
    //  视频列表
    wx.request({
      url: api.video.list,
      data: {
        grade_id: page.data.grade_id,
        course_id: page.data.course_id,
        knowledg_ids: page.data.knowledg_ids,
        per_page: 9999,
      },
      success(res) {
        if (res.data.data !== '' && res.data.data.list) {
          //  处理视频的时长
          for (var index in res.data.data.list) {
            res.data.data.list[index].duration = (res.data.data.list[index].duration / 60).toFixed(2).replace(".", ":");
          }
          // 设置数据  
          page.setData({
            video_list: res.data.data.list
          });

        } else {
          page.setData({
            video_list: []
          });
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})